const express = require("express");
const mongoose = require("mongoose");
const moment = require("moment");
const app = express();
app.post("/weibo",(req,res)=>{
	const {context} = req.body;
	// 1- 连接数据库
	// 2- 操作数据-schema model
	// 3- 返回结果
	mongoose.connect("mongodb://127.0.0.1:27017/weibo",{
		serverSelectionTimeoutMS:10000
	}).then(value=>{
		// 连接成功
		const schema = new mongoose.Schema({
			context:{
				type:String,
				unique:true,
				required:true
			},
			addTime:{
				type:String,
				required:true
			},
			topNum:{
				type:Number,
				default:0
			},
			downNum:{
				type:Number,
				default:0
			}
		});
		const model = mongoose.model("contentList",schema,"contentList");
		model({
			context,
			addTime:moment().format("YYYY-MM-DD hh:mm:ss")
		}).save().then(value=>{
			res.json({
				ok:1,
				msg:"添加成功"
			})
		}).catch(err=>{
			res.json({
				ok:-1,
				msg:"添加失败"
			})
		})
	});
	
	
})
app.listen(8090,()=>{
	console.log("success");
})